﻿Jxstar.currentPage = function() {
	var config = {param:{},initpage:function(page, define){},eventcfg:{}};

	var auditData = Jxstar.findComboData('audit');
	var planstatusData = Jxstar.findComboData('planstatus');
	var plantypeData = Jxstar.findComboData('plantype');
	var moneytypeData = Jxstar.findComboData('moneytype');
	var yesnoData = Jxstar.findComboData('yesno');
	var planyearData = Jxstar.findComboData('planyear');

	var cols = [
	{col:{header:'记录状态', width:72, sortable:true, hidden:true, align:'center',
		renderer:function(value){
			for (var i = 0; i < auditData.length; i++) {
				if (auditData[i][0] == value)
					return auditData[i][1];
			}
		}}, field:{name:'money_plan__audit_up',type:'string'}},
	{col:{header:'计划状态', width:67, sortable:true, align:'center',
		editable:false,
		editor:new Ext.form.ComboBox({
			store: new Ext.data.SimpleStore({
				fields:['value','text'],
				data: planstatusData
			}),
			emptyText: jx.star.select,
			mode: 'local',
			triggerAction: 'all',
			valueField: 'value',
			displayField: 'text',
			editable:false,
			value: planstatusData[0][0]
		}),
		renderer:function(value){
			for (var i = 0; i < planstatusData.length; i++) {
				if (planstatusData[i][0] == value)
					return planstatusData[i][1];
			}
		}}, field:{name:'money_plan__plan_status',type:'string'}},
	{col:{header:'项目名称', width:165, sortable:true}, field:{name:'money_plan__plan_name',type:'string'}},
	{col:{header:'主要内容与申请理由', width:260, sortable:true}, field:{name:'money_plan__plan_desc',type:'string'}},
	{col:{header:'预计总投资(万元)', width:117, sortable:true, renderer:JxUtil.formatNumber(6)}, field:{name:'money_plan__plan_money',type:'float'}},
	{col:{header:'当年预计付款(万元)', width:119, sortable:true, renderer:JxUtil.formatNumber(6)}, field:{name:'money_plan__year_money',type:'float'}},
	{col:{header:'计划类型', width:78, sortable:true, hidden:true, align:'center',
		renderer:function(value){
			for (var i = 0; i < plantypeData.length; i++) {
				if (plantypeData[i][0] == value)
					return plantypeData[i][1];
			}
		}}, field:{name:'money_plan__plan_type',type:'string'}},
	{col:{header:'项目类别', width:95, sortable:true, align:'center',
		editable:false,
		editor:new Ext.form.ComboBox({
			store: new Ext.data.SimpleStore({
				fields:['value','text'],
				data: moneytypeData
			}),
			emptyText: jx.star.select,
			mode: 'local',
			triggerAction: 'all',
			valueField: 'value',
			displayField: 'text',
			editable:false, allowBlank:false,
			value: moneytypeData[0][0]
		}),
		renderer:function(value){
			for (var i = 0; i < moneytypeData.length; i++) {
				if (moneytypeData[i][0] == value)
					return moneytypeData[i][1];
			}
		}}, field:{name:'money_plan__money_type',type:'string'}},
	{col:{header:'需报国家局', width:78, sortable:true, hidden:true, align:'center',
		renderer:function(value){
			for (var i = 0; i < yesnoData.length; i++) {
				if (yesnoData[i][0] == value)
					return yesnoData[i][1];
			}
		}}, field:{name:'money_plan__up_comp',type:'string'}},
	{col:{header:'数量', width:100, sortable:true, hidden:true, renderer:JxUtil.formatNumber(2)}, field:{name:'money_plan__plan_num',type:'float'}},
	{col:{header:'单位', width:100, sortable:true, hidden:true}, field:{name:'money_plan__plan_unit',type:'string'}},
	{col:{header:'单价(万元)', width:100, sortable:true, hidden:true, renderer:JxUtil.formatNumber(6)}, field:{name:'money_plan__plan_price',type:'float'}},
	{col:{header:'型号规格', width:100, sortable:true}, field:{name:'money_plan__device_size',type:'string'}},
	{col:{header:'拟选供应商', width:100, sortable:true}, field:{name:'money_plan__provider',type:'string'}},
	{col:{header:'预计实施时间', width:100, sortable:true}, field:{name:'money_plan__plan_period',type:'string'}},
	{col:{header:'预算年度', width:64, sortable:true, align:'center',
		editable:false,
		editor:new Ext.form.ComboBox({
			store: new Ext.data.SimpleStore({
				fields:['value','text'],
				data: planyearData
			}),
			emptyText: jx.star.select,
			mode: 'local',
			triggerAction: 'all',
			valueField: 'value',
			displayField: 'text',
			editable:false,
			value: planyearData[0][0]
		}),
		renderer:function(value){
			for (var i = 0; i < planyearData.length; i++) {
				if (planyearData[i][0] == value)
					return planyearData[i][1];
			}
		}}, field:{name:'money_plan__plan_year',type:'string'}},
	{col:{header:'申报日期', width:100, sortable:true, renderer:function(value) {
			return value ? value.format('Y-m-d') : '';
		}}, field:{name:'money_plan__plan_date',type:'date'}},
	{col:{header:'申报人', width:73, sortable:true}, field:{name:'money_plan__plan_user',type:'string'}},
	{col:{header:'申报部门', width:100, sortable:true}, field:{name:'money_plan__dept_name',type:'string'}},
	{col:{header:'计划编号', width:100, sortable:true}, field:{name:'money_plan__plan_code',type:'string'}},
	{col:{header:'备注', width:100, sortable:true, hidden:true}, field:{name:'money_plan__plan_memo',type:'string'}},
	{col:{header:'主键', width:100, sortable:true, hidden:true}, field:{name:'money_plan__plan_id',type:'string'}},
	{col:{header:'申报人ID', width:100, sortable:true, hidden:true}, field:{name:'money_plan__plan_userid',type:'string'}},
	{col:{header:'申报部门ID', width:100, sortable:true, hidden:true}, field:{name:'money_plan__dept_id',type:'string'}},
	{col:{header:'生产厂家', width:100, sortable:true}, field:{name:'money_plan__create_factory',type:'string'}},
	{col:{header:'固定资产编号', width:100, sortable:true}, field:{name:'money_plan__asset_code',type:'string'}},
	{col:{header:'出厂日期', width:100, sortable:true}, field:{name:'money_plan__create_date',type:'string'}},
	{col:{header:'记录状态-审批用', width:100, sortable:true, hidden:true}, field:{name:'money_plan__auditing',type:'string'}}
	];
	
	config.param = {
		cols: cols,
		sorts: null,
		hasQuery: '1',
		isedit: '0',
		isshow: '1',
		funid: 'money_pass'
	};
	
	config.eventcfg = {
	//创建投资计划包
	newbao: function(){
		var records = this.grid.getSelectionModel().getSelections();
		if (!JxUtil.selected(records)) return;
		
		if (records.length < 2) {
			JxHint.alert('选择的投资计划数量必须大于1条，才能合并！');
			return;
		}
		
		//检查投资计划的有效性，必须是同时期、同单位、同类别的计划
		var old_money_type = '', old_dept_id = '', old_plan_year = '';
		for (var i = 0; i < records.length; i++) {
			var dept_id = records[i].get('money_plan__dept_id');
				dept_id = dept_id.substr(0, 4);
			var plan_year = records[i].get('money_plan__plan_year');
			var money_type = records[i].get('money_plan__money_type');
			
			if (old_dept_id.length == 0) {
				old_dept_id = dept_id;
			} else {
				if (old_dept_id != dept_id) {
					JxHint.alert('选择的投资计划申请单位不同，不能合并！');
					return;
				}
			}
			
			if (old_plan_year.length == 0) {
				old_plan_year = plan_year;
			} else {
				if (old_plan_year != plan_year) {
					JxHint.alert('选择的投资计划预算年度不同，不能合并！');
					return;
				}
			}
			
			if (old_money_type.length == 0) {
				old_money_type = money_type;
			} else {
				if (old_money_type != money_type) {
					JxHint.alert('选择的投资计划项目类别不同，不能合并！');
					return;
				}
			}
		}
		
		var planids = '';
		for (var i = 0; i < records.length; i++) {
			var planid = records[i].get('money_plan__plan_id');
			planids += '&keyid=' + planid
		}
		
		this.createWindow(planids);
	},
	
	exebao: function(planids, form) {			
		var params = 'funid=money_pass' + planids;
		params += JxUtil.getFormValues(form, false);
		params += '&pagetype=grid&eventcode=newbao';
		
		var self = this;
		var endcall = function(data) {
			//重新加载数据
			self.grid.getStore().reload();
		};

		//发送请求
		Request.postRequest(params, endcall);
	},
	
	//创建计划包对话框
	createWindow: function(planids) {
		var plantype2Data = Jxstar.findComboData('plantype2');
	
		var dataForm = new Ext.form.FormPanel({
			id: 'node_money_pass_subform',
			style: 'padding:20px;',
			border: false,
			frame: false,
			baseCls: 'x-plain',
			items: [{
				anchor:'100%',
				border: false,
				layout:'column',
				autoHeight:true,
				items:[{
					border:false,
					columnWidth:0.5,
					layout:'form',
					style: 'padding-left:10px;',
					items:[
						{xtype:'combo', fieldLabel:'国家局项目类别', name:'money_plan__money_type2',
						anchor:'100%', editable:false, allowBlank:false, labelStyle:'color:#0000FF;', labelSeparator:'*',
							store: new Ext.data.SimpleStore({
								fields:['value','text'],
								data: plantype2Data
							}),
							emptyText: jx.star.select,
							mode: 'local',
							triggerAction: 'all',
							valueField: 'value',
							displayField: 'text',
							value: plantype2Data[0][0]},
						{xtype:'textfield', fieldLabel:'项目名称', name:'money_plan__plan_name', allowBlank:false, labelStyle:'color:#0000FF;', labelSeparator:'*', anchor:'100%', maxLength:50}						
					]
				},{
					border:false,
					columnWidth:0.5,
					layout:'form',
					style: 'padding-left:10px;',
					items:[
						{xtype:'trigger', fieldLabel:'申报单位', name:'money_plan__dept_name', 
							anchor:'100%', triggerClass:'x-form-search-trigger',
							maxLength:50, editable:false, allowBlank:false, labelStyle:'color:#0000FF;', labelSeparator:'*',
							onTriggerClick: function() {
								var selcfg = {pageType:'combogrid', nodeId:'sys_dept', layoutPage:'/public/layout/layout_tree.js', sourceField:'sys_dept.dept_name;dept_id', targetField:'money_plan.dept_name;dept_id', whereSql:"", whereValue:'', whereType:'', isSame:'0', isShowData:'1', isMoreSelect:'0',isReadonly:'1',fieldName:'money_plan.dept_name'};
								JxSelect.createSelectWin(selcfg, this, 'node_money_pass_subform');
							}},
						{xtype:'textfield', fieldLabel:'创建人', name:'money_plan__plan_user', readOnly:true, anchor:'100%', maxLength:20},
						{xtype:'hidden', fieldLabel:'创建人ID', name:'money_plan__plan_userid', anchor:'100%'},
						{xtype:'hidden', fieldLabel:'申报部门ID', name:'money_plan__dept_id', anchor:'100%'}
					]
				}
				]
			},{
				anchor:'100%',
				border: false,
				layout:'column',
				autoHeight:true,
				items:[{
					border:false,
					columnWidth:1,
					layout:'form',
					style: 'padding-left:10px;',
					items:[
						{xtype:'textarea', fieldLabel:'建设规模和建设内容', name:'money_plan__plan_desc', allowBlank:false, labelStyle:'color:#0000FF;', labelSeparator:'*', width:'100%', height:48, maxLength:200},
						{xtype:'textarea', fieldLabel:'年度项目实施内容', name:'money_plan__year_content2', width:'100%', height:48, maxLength:200}
					]
				}
				]
			}]
		});
		
		//添加缺省值
		var form = dataForm.getForm();
		form.set('money_plan__dept_id', JxDefault.getOrgId());
		form.set('money_plan__dept_name', JxDefault.getOrgName());
		form.set('money_plan__plan_user', JxDefault.getUserName());
		form.set('money_plan__plan_userid', JxDefault.getUserId());
		
		var self = this;
		//创建对话框
		var win = new Ext.Window({
			title:	'创建投资计划包',
			layout:'fit',
			width:700,
			height:270,
			resizable: false,
			modal: true,
			closeAction:'close',
			items:[dataForm],

			buttons: [{
				text:jx.base.ok,		//'确定'
				handler:function(){
					//数据校验
					if (!form.isValid()) {
						JxHint.alert(jx.event.datavalid);	//'请确保输入的数据正确完整！'
						return;
					}
					self.exebao(planids, form);
					win.close();
				}
			},{
				text:jx.base.cancel,	//'取消'
				handler:function(){win.close();}
			}]
		});
		win.show();
	},

	//单个备案
	newmemo: function(){
		var records = this.grid.getSelectionModel().getSelections();
		if (!JxUtil.selected(records)) return;
		
		var self = this;
		var hdcall = function() {
			//取选择记录的主键值
			var params = 'funid='+ self.define.nodeid;
			for (var i = 0; i < records.length; i++) {
				params += '&keyid=' + records[i].get(self.define.pkcol);
			}

			//设置请求的参数
			params += '&pagetype=grid&eventcode=newmemo';

			//执行处理的内容
			var endcall = function(data) {
				//重新加载数据
				self.grid.getStore().reload();
			};

			//发送请求
			Request.postRequest(params, endcall);
		};
		
		Ext.Msg.confirm(jx.base.hint, '选择的这些投资计划将单个提交国家局备案，确定吗？', function(btn) {
			if (btn == 'yes') hdcall();
		});
	},
	
	//等待授权
	newup: function(){
		var records = this.grid.getSelectionModel().getSelections();
		if (!JxUtil.selected(records)) return;
		
		var self = this;
		var hdcall = function() {
			//取选择记录的主键值
			var params = 'funid='+ self.define.nodeid;
			for (var i = 0; i < records.length; i++) {
				params += '&keyid=' + records[i].get(self.define.pkcol);
			}

			//设置请求的参数
			params += '&pagetype=grid&eventcode=newup';

			//执行处理的内容
			var endcall = function(data) {
				//重新加载数据
				self.grid.getStore().reload();
			};

			//发送请求
			Request.postRequest(params, endcall);
		};
		
		Ext.Msg.confirm(jx.base.hint, '选择的这些投资计划将不提交国家局备案，等待授权，确定吗？', function(btn) {
			if (btn == 'yes') hdcall();
		});
	}
};
		
	return new Jxstar.GridNode(config);
}